package com.example.elderlycare.controller;

import com.example.elderlycare.controller.entity.EvaluationRequestBody;
import com.example.elderlycare.dao.entity.Evaluation;
import com.example.elderlycare.service.EvaluationService;
import com.example.elderlycare.service.entity.EvaluationListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/evaluation")
public class EvaluationController {

    @Resource
    EvaluationService evaluationService;

    /**
     * 查询评估登记记录
     *
     * @param elderName      老人姓名
     * @param evaluateTypeId 评估类型id
     * @param standardId     评估标准id
     * @param register       登记人
     * @param pageSize       页大小
     * @param current        当前页
     * @return EvaluationResponseBody
     */
    @GetMapping("/query")
    public Result<EvaluationListResponseBody> queryEvaluation(
            @RequestParam(value = "elderName", required = false) String elderName,
            @RequestParam(value = "evaluateTypeId", required = false) String evaluateTypeId,
            @RequestParam(value = "standardId", required = false) String standardId,
            @RequestParam(value = "register", required = false) String register,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("elderName", elderName != null ? "%" + elderName + "%" : null);
        params.put("evaluateTypeId", evaluateTypeId);
        params.put("standardId", standardId);
        params.put("register", register != null ? "%" + register + "%" : null);
        params.put("limit", pageSize);
        params.put("offset", (current - 1) * pageSize);
        try {
            EvaluationListResponseBody res = evaluationService.queryEvaluations(params);
            return Result.success(res);
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 新增评估登记记录
     *
     * @param req EvaluationRequestBody.addEvaluation
     * @return null
     */
    @PostMapping("/add")
    public Result<?> addEvaluation(@RequestBody EvaluationRequestBody.addEvaluation req) {
        Evaluation evaluation = new Evaluation();
        BeanUtils.copyProperties(req, evaluation);
        evaluation.setDate(new Date());
        try {
            boolean flag = evaluationService.save(evaluation);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("add Evaluation error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 修改评估登记记录接口
     *
     * @param req EvaluationRequestBody.updateEvaluation
     * @return null
     */
    @PutMapping("/update")
    public Result<?> updateEvaluation(@RequestBody EvaluationRequestBody.updateEvaluation req) {
        Evaluation evaluation = new Evaluation();
        BeanUtils.copyProperties(req, evaluation);
        try {
            boolean flag = evaluationService.updateById(evaluation);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("update Evaluation error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 删除评估登记记录
     *
     * @param id 记录id
     * @return null
     */
    @DeleteMapping("/delete")
    public Result<?> deleteEvaluation(@RequestParam("id") @NotEmpty Integer id) {
        try {
            boolean flag = evaluationService.removeById(id);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("delete Evaluation error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }
}
